<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ajax</title>
</head>
<body>
    <h1>Ajax</h1>
    <script>
        function getJson(url) {
            return new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open('GET', url);
                xhr.setRequestHeader('Accept', 'application/json');
                xhr.responseType = 'json';
                xhr.onreadystatechange = function() {
                    if( this.readyState !== 4 ) return;
                    if( this.status === 200 ) {
                        resolve(xhr.response);
                    }
                    else {
                        reject(xhr.statusText);
                    }
                };
                xhr.send();
            });
        }

        const url = 'demo.txt';
        getJson(url).then(res => {
            console.log(res);
        })
    </script>
</body>
</html>